package org.sgsm.dungeon.generator.method;

import org.sgsm.dungeon.generator.ObjectType;

public class MethodFind extends MethodCRUD
{
  public String _objetList = "";

  public MethodFind()
  {
    super();
  }

  public void generateServiceImpl()
  {
    print(getSignature());
    up();
    print(getObjetOut().getName() + " result = null;");
    print("if( p" + getBean() + " != null)");
    up();
    print("result = new " + getObjetOut().getName() + "();");
    print("Session session = getSession();");
    print("Criteria crit = session.createCriteria(" + getBean() + ".class);");
    //crit.add( Restrictions.eq( "color", eg.Color.BLACK ) );");
    print("crit.setMaxResults(10);");
    print("List<" + _objetList + "> resultCrit = crit.list();");
    print("Iterator<" + _objetList + "> it = resultCrit.iterator();");
    print("while(it.hasNext())");
    up();
    print("result.getValues().add(it.next());");
    down();
    down();
    print("return result;");
    down();
  }

  public void generateName(String pPack, String pName)
  {
    _ObjetOut = new ObjectType(pPack, pName + "ListType");
    _ObjetIn.add(new ObjectType(pPack, pName + "CriteriaType"));
    _ObjetIn.add(new ObjectType(pPack, pName + "ScenarioType"));
    _ObjetIn.add(getContext());
    setMethodName("find" + pName);
    _objetList = pName + "Type";
  }
}
